from urllib import request
import re
import os

url = "http://www.ivsky.com/"

# 发送请求
response = request.urlopen(url)
# 返回数据
html = response.read().decode('utf-8')
# 创建路径
targetPath = os.getcwd() + os.path.sep + 'a' + os.path.sep
# 判断路径存不存在
if not os.path.exists(targetPath):
    # 创建文件夹
    os.makedirs(targetPath)
# print(html)

# 定义规则
pat = re.compile (r'<img src="(.*?)".*?>')
res = pat.findall(html)
print(res)
# 下载图片
for item in res:
    targetPath2 = (targetPath+item.split("/")[-1])

    print(item)
    request.urlretrieve(item,targetPath2)

# print(os.path.abspath('./a/item.split("/")[-1]'))